package com.example.iread.adapter;

import android.content.Context;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.iread.R;
import com.example.iread.bean.BookContentPageBean;

import java.util.ArrayList;
import java.util.List;

public class ReaderAdapter extends RecyclerView.Adapter<ReaderAdapter.MyViewHolder> {

    private static String TAG = "SoRIya";

    private final Context context;

    private List<BookContentPageBean> list;

    private String content;

    public ReaderAdapter(Context context, String content) {
        this.context = context;
        this.content = content;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_reader, parent, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        TextView tvReaderContent = holder.tv_reader_content;

        if (list == null) {
            tvReaderContent.post(() -> {
                list = getPage(content, "", tvReaderContent);
                BookContentPageBean bookContentPageBean = list.get(position);
                tvReaderContent.setText(bookContentPageBean.pageContent);
                content = null; // 销毁
            });
            return;
        }
        if (position == list.size() - 1) {
            tvReaderContent.setGravity(Gravity.CENTER);
            tvReaderContent.getPaint().setFakeBoldText(true);
        }
        tvReaderContent.setText(list.get(position).pageContent);
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 1;
    }
    public static List<BookContentPageBean> getPage(@NonNull String content,  String title, TextView textView) {
        textView.setText(content);
        int count = textView.getLineCount(); // 获取TV总行数
        int pageLineCount = getPageLineCount(textView); // 获取页面可以容纳的行数
        int pageCount = (int) Math.ceil(((double) count / (double) pageLineCount)); // 获取页面数

        // 将长文本根据页面大小分割，装载进List
        ArrayList<BookContentPageBean> pages = new ArrayList<>();
        for (int i = 0; i < pageCount; i++) {
            BookContentPageBean currentPage = new BookContentPageBean();
            int startPos = 0;
            int endPos = content.length();
            // 不是第一个，当前页的startPos为上一页的endPos
            if (i != 0) startPos = pages.get(i - 1).endPos;
            // 不是最后一个
            if (i != pageCount - 1) endPos = textView.getLayout().getLineEnd((i + 1) * pageLineCount - 1);
            currentPage.startPos = startPos;
            currentPage.endPos = endPos;
            currentPage.pageId = i;
            if (i == 0) {
                currentPage.isFirstPage = true;
                currentPage.title = title;
            }
            currentPage.pageContent = content.substring(startPos, endPos);
            currentPage.isTempPage = false;
            pages.add(currentPage);
        }
        BookContentPageBean bookContentPage = new BookContentPageBean();
        bookContentPage.pageContent = "This chapter has ended";
        pages.add(bookContentPage);
        return pages;
    }

    private static int getPageLineCount(TextView textView) {
        int vHeight = textView.getBottom() - textView.getTop();
        int firstHeight = getLineHeight(0, textView);
        int otherHeight = getLineHeight(1, textView);
        int lines = 1;
//        if (otherHeight != 0) lines = (vHeight - firstHeight) / otherHeight + 1;
        if (otherHeight != 0) lines = (vHeight - firstHeight) / otherHeight;
        return lines;
    }

    private static int getLineHeight(int line, TextView textView) {
        Rect rect = new Rect();
        textView.getLineBounds(line, rect);
        return rect.bottom - rect.top;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_reader_content;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_reader_content = itemView.findViewById(R.id.tv_reader_content);
        }
    }

}
